package com.aidong.service;

/**
 * Created by john on 16-11-2.
 */
import com.aidong.dao.wills.PayListDao;
import com.aidong.model.wills.ContractInfo;
import com.aidong.util.Bj004Util;
import com.aidong.util.Sh063Util;
import com.aidong.util.Sh070Util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;

import com.aidong.web.controller.BaseController;
import org.springframework.stereotype.Service;
import sun.net.ftp.FtpClient;

@Service
public class ReportUploadService extends BaseController {
    @Resource
    PayListDao payListDao;

    public ReportUploadService() {
    }

    public boolean doWanXiangUpload(String date) {
        try {
            SimpleDateFormat e = new SimpleDateFormat("yyyy-MM-dd");
            if(this.chkNull(date)) {
                Calendar dt = Calendar.getInstance();
                dt.add(5, -1);
                date = e.format(dt.getTime());
            }

            Date dt1 = e.parse(date);
            List list = this.payListDao.getListBySaleTypeWanXiang("HZ002", this.parseDate(date, "yyyy-MM-dd"), this.parseDate(date + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
            SimpleDateFormat sdf1 = new SimpleDateFormat("HHmm");
            DecimalFormat nf = new DecimalFormat("0.##");
            StringBuilder sb = new StringBuilder();
            Iterator i$ = list.iterator();

            while(i$.hasNext()) {
                ContractInfo data = (ContractInfo)i$.next();
                if(data.getAmount1().doubleValue() + data.getAmount2().doubleValue() + data.getAmount3().doubleValue() != 0.0D) {
                    String id = "00000000" + data.getSaleId();
                    id = data.getSaleType() + id.substring(id.length() - 8);
                    sb.append("S16601\t01\t").append(e.format(data.getSaleDate())).append("\t").append(sdf1.format(data.getSaleDate())).append("\t").append(id).append("\tS16601YL0001\t\t").append(nf.format(data.getAmount1())).append("\t").append(nf.format(data.getAmount2())).append("\t0\t").append(nf.format(data.getAmount3())).append("\t\t0\t").append(nf.format(data.getAmount1().doubleValue() + data.getAmount2().doubleValue() + data.getAmount3().doubleValue())).append("\t\r\n");
                }
            }

            sdf1 = new SimpleDateFormat("yyyyMMdd");
            this.uploadFtp("ftp.crshk.com.cn", 21, "S16601", "S16601", "", "S16601" + sdf1.format(dt1) + ".txt", sb.toString(), "gbk");
        } catch (Exception var11) {
            this.log.error("doWanXiangUpload", var11);
        }

        return false;
    }

    public boolean doSH061Upload(String date) {
        try {
            SimpleDateFormat e = new SimpleDateFormat("yyyy-MM-dd");
            if(this.chkNull(date)) {
                Calendar dt = Calendar.getInstance();
                dt.add(5, -1);
                date = e.format(dt.getTime());
            }

            e.parse(date);
            List list = this.payListDao.getListBySaleTypeSH061("SH061", this.parseDate(date, "yyyy-MM-dd"), this.parseDate(date + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
            e = new SimpleDateFormat("yyyyMMdd");
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            DecimalFormat nf = new DecimalFormat("0.00");
            int n = 1;
            StringBuilder sb = new StringBuilder();
            Iterator i$ = list.iterator();

            while(i$.hasNext()) {
                ContractInfo data = (ContractInfo)i$.next();
                double money = data.getAmount1().doubleValue() + data.getAmount2().doubleValue() + data.getAmount3().doubleValue();
                if(money != 0.0D) {
                    String id = "0000" + n++;
                    id = e.format(data.getSaleDate()) + id.substring(id.length() - 4);
                    sb.append("S-F305,136,").append(id).append(",004382,威尔士健身零售,").append(money < 0.0D?-1:1).append(",").append(money).append(",0.00,").append(nf.format(data.getAmount1())).append(",").append(nf.format(data.getAmount2())).append(",0.00,0.00,").append(nf.format(data.getAmount3())).append(",").append(sdf1.format(data.getSaleDate())).append("\r\n");
                }
            }

            sdf1 = new SimpleDateFormat("yyyyMMddHHmmss");
            this.writeFile(this.getSetting("downloadPath") + "upftp", sdf1.format(new Date()) + ".txt", sb.toString());
            this.uploadFtp("58.247.109.130", '썒', "S-F305", "wesjs136", "wesjs", sdf1.format(new Date()) + ".txt", sb.toString(), "gbk");
        } catch (Exception var14) {
            this.log.error("doSH061Upload", var14);
        }

        return false;
    }

    public boolean doSH063Upload(String date) {
        try {
            SimpleDateFormat e = new SimpleDateFormat("yyyy-MM-dd");
            if(this.chkNull(date)) {
                Calendar dt = Calendar.getInstance();
                dt.add(5, -1);
                date = e.format(dt.getTime());
            }

            e.parse(date);
            List list = this.payListDao.getListBySaleTypeSH063("SH063", this.parseDate(date, "yyyy-MM-dd"), this.parseDate(date + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
            e = new SimpleDateFormat("yyyyMMdd");
            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            new DecimalFormat("0.00");
            int n = 0;
            double mm = 0.0D;
            String id = null;
            Iterator i$ = list.iterator();

            while(i$.hasNext()) {
                ContractInfo data = (ContractInfo)i$.next();
                double money = data.getAmount1().doubleValue() + data.getAmount2().doubleValue() + data.getAmount3().doubleValue();
                if(money != 0.0D) {
                    mm += money;
                    ++n;
                    id = "0000" + n;
                    id = e.format(data.getSaleDate()) + id.substring(id.length() - 4);
                    Date var16 = data.getSaleDate();
                    Sh063Util.postSales(data, id);
                }
            }
        } catch (Exception var15) {
            this.log.error("doSh063Upload", var15);
        }

        return false;
    }

    public boolean doSH070Upload(String date) {
        try {
            SimpleDateFormat e = new SimpleDateFormat("yyyy-MM-dd");
            if(this.chkNull(date)) {
                Calendar dt = Calendar.getInstance();
                dt.add(5, -1);
                date = e.format(dt.getTime());
            }

            e.parse(date);
            List list = this.payListDao.getListBySaleTypeSH063("SH070", this.parseDate(date, "yyyy-MM-dd"), this.parseDate(date + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
            e = new SimpleDateFormat("yyyyMMdd");
            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            new DecimalFormat("0.00");
            int n = 0;
            double mm = 0.0D;
            String id = null;
            Iterator i$ = list.iterator();

            while(i$.hasNext()) {
                ContractInfo data = (ContractInfo)i$.next();
                double money = data.getAmount1().doubleValue() + data.getAmount2().doubleValue() + data.getAmount3().doubleValue();
                if(money != 0.0D) {
                    mm += money;
                    ++n;
                    id = "0000" + n;
                    id = e.format(data.getSaleDate()) + id.substring(id.length() - 4);
                    Date var16 = data.getSaleDate();
                    Sh070Util.postSales(data, id);
                }
            }
        } catch (Exception var15) {
            this.log.error("doSh070Upload", var15);
        }

        return false;
    }

    public boolean doBJ004Upload(String date) {
        try {
            SimpleDateFormat e = new SimpleDateFormat("yyyy-MM-dd");
            if(this.chkNull(date)) {
                Calendar dt = Calendar.getInstance();
                dt.add(5, -1);
                date = e.format(dt.getTime());
            }

            e.parse(date);
            List list = this.payListDao.getListBySaleTypeBJ004("BJ004", this.parseDate(date, "yyyy-MM-dd"), this.parseDate(date + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
            e = new SimpleDateFormat("yyyyMMdd");
            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            new DecimalFormat("0.00");
            int n = 0;
            double mm = 0.0D;
            String id = null;
            Iterator i$ = list.iterator();

            while(i$.hasNext()) {
                ContractInfo data = (ContractInfo)i$.next();
                double money = data.getAmount1().doubleValue() + data.getAmount2().doubleValue() + data.getAmount3().doubleValue();
                if(money != 0.0D) {
                    mm += money;
                    ++n;
                    id = "0000" + n;
                    id = e.format(data.getSaleDate()) + id.substring(id.length() - 4);
                    Date var16 = data.getSaleDate();
                    Bj004Util.postSales(data, id);
                }
            }
        } catch (Exception var15) {
            this.log.error("doBJ004Upload", var15);
        }

        return false;
    }

    private void writeFile(String path, String fileName, String content) {
        try {
            File ee = new File(path);
            if(!ee.exists()) {
                ee.mkdir();
            }

            FileOutputStream fos = new FileOutputStream(path + File.separator + fileName);
            fos.write(content.getBytes("gbk"));
            fos.close();
        } catch (IOException var6) {
            this.log.error("writeFile", var6);
        }

    }

    private void uploadFtp(String ftpHost, int port, String ftpUser, String ftpPwd, String path, String fileName, String content, String encode) throws Exception {
        FtpClient ftpClient;
        try {
            ftpClient = FtpClient.create();
            ftpClient.setConnectTimeout(30000);
            ftpClient.setReadTimeout(30000);
            ftpClient.connect(new InetSocketAddress(ftpHost, port));
        } catch (IOException var11) {
            this.log.error("connect to " + ftpHost, var11);
            return;
        }

        ftpClient.login(ftpUser, ftpPwd.toCharArray());
        if(path != null && path.length() > 0) {
            ftpClient.changeDirectory(path);
        }

        ftpClient.setBinaryType();
        OutputStream os = ftpClient.putFileStream(fileName, true);
        os.write(content.getBytes(encode));
        os.close();
        ftpClient.close();
    }
}

